package cn.xfdzx.android.apps.shop.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.adapter.ManagementAddressListAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AddAddressBeen;
import cn.xfdzx.android.apps.shop.bean.AddressListBeen;
import cn.xfdzx.android.apps.shop.bean.DeleteAddressBeen;
import cn.xfdzx.android.apps.shop.contract.AddressListContract;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.AddressListPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.SlideRecyclerView;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;
import static cn.xfdzx.android.apps.shop.apps.Constants.START_CODE;

public class ManagementAddressActivity extends BaseMvpActivity<AddressListPresenter> implements AddressListContract.View {

    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.rv_address_list)
    SlideRecyclerView rvAddressList;
    @BindView(R.id.tv_bottom_add_address)
    TextView tvAddAddress;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.ll_hint)
    LinearLayout llHint;

    private RelativeLayout.LayoutParams item_bar;
    private int page = 1;
    private List<AddressListBeen.DataBean> mList = new ArrayList<>();
    private ManagementAddressListAdapter managementAddressListAdapter;
    private Activity mContext;
    private String from;


    @Override
    public int getLayoutId() {
        return R.layout.activity_management_address;
    }

    @Override
    public void initView() {
        mContext = this;
        initHigh();
        mPresenter = new AddressListPresenter();
        mPresenter.attachView(this);
        page = 1;
        mPresenter.initAddressData(true, false, page + "",200+"");

        from = getIntent().getStringExtra("from");
        // 商铺列表
        rvAddressList.setLayoutManager(new LinearLayoutManager(this));
        rvAddressList.setItemAnimator(new DefaultItemAnimator());
        managementAddressListAdapter = new ManagementAddressListAdapter(mContext, mList, mPresenter);
        managementAddressListAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (from.equals("confirm")) {
                    Intent intent = new Intent();
                    intent.putExtra("data", mList.get(position));
                    setResult(RESULT_OK, intent);
                    finish();
                }

            }
        });
        rvAddressList.setAdapter(managementAddressListAdapter);

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                page = 1;
                mPresenter.initAddressData(true, true, page + "",200+"");

            }
        });

    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onInitSuccess(AddressListBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mList.clear();
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.initAddressData(false, false, page + "",200+"");
                    }
                });
            }
            if (bean.getData().size() == 0) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }
            mList.addAll(bean.getData());
            managementAddressListAdapter.notifyDataSetChanged();
            if (mList.size() == 0) {
                llHint.setVisibility(View.VISIBLE);
            } else {
                llHint.setVisibility(View.GONE);
            }
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onDelectSuccess(DeleteAddressBeen bean, int position) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mList.remove(position);
            managementAddressListAdapter.notifyDataSetChanged();
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onAddAddressSuccess(AddAddressBeen bean, int position, int index) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            mList.get(index).setIs_default(0);
            mList.get(position).setIs_default(1);
            managementAddressListAdapter.notifyDataSetChanged();
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }


    @OnClick({R.id.tv_bottom_add_address, R.id.iv_location_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_bottom_add_address:
                Intent intent = new Intent(mContext, AddAddressActivity.class);
                intent.putExtra("from", "management");
                intent.putExtra("is_default", "0");
                intent.putExtra("id", "");
                startActivityForResult(intent, START_CODE);
                break;
            case R.id.iv_location_back:
                finish();
                break;
        }
    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == START_CODE) {
                page = 1;
                mPresenter.initAddressData(true, true, page + "",200+"");
            }
        }
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }
}
